//
//  ContactSearchViewContactCell.m
//  Showing
//
//  Created by Ralf on 15/4/27.
//  Copyright (c) 2015年 next-software. All rights reserved.
//

#import "ContactSearchViewContactCell.h"
#import <SDWebImage/UIImageView+WebCache.h>

#import "AppDelegate.h"

@interface ContactSearchViewContactCell ()
{
    AppDelegate *appDelegate;
}
@end

@implementation ContactSearchViewContactCell

- (void)awakeFromNib
{
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    ContactSearchViewContactCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CONTACT"];

    if (cell == nil)
    {
        cell = [[NSBundle mainBundle] loadNibNamed:@"ContactSearchViewContactCell" owner:nil options:nil][0];
    }

    return cell;
}

- (void)setData:(NSDictionary *)data
{
    appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];

    UILabel *lAlias = (UILabel *) [self viewWithTag:20];
    lAlias.text = data[@"alias"];

    UIImageView *ivAvatar = (UIImageView *) [self viewWithTag:10];

    ivAvatar.layer.borderColor = [[UIColor blackColor] CGColor];
    ivAvatar.layer.borderWidth = 1.0;

    if ([data[@"thumbnailURL"] isEqual:[NSNull null]])
    {
        ivAvatar.image = [UIImage imageNamed:@"Avatar-Default.png"];
    }
    else
    {
        [self loadAvatar:data[@"thumbnailURL"]];
    }
}

- (void)loadAvatar:(NSString *)thumbnailURL
{
    UIImageView *ivAvatar = (UIImageView *) [self viewWithTag:10];
    NSString    *url      = [NSString stringWithFormat:@"%@/%@", appDelegate.parameterManager.server, thumbnailURL];
    [ivAvatar sd_setImageWithURL:[NSURL URLWithString:url]];
}

@end
